package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.Department;
import com.rzjm.exercise.module.domain.Shop;
import com.rzjm.exercise.module.domain.Towns;
import com.rzjm.exercise.module.service.DepartmentService;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.module.service.TownsService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import com.rzjm.exercise.sys.system.user.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2020-12-15 22:36:23
 */
@Controller
@RequestMapping(value = "/module/department",produces ="application/json;charset=UTF-8")
public class DepartmentController extends BaseController {
    @Resource
    private DepartmentService departmentService;
    @Resource
    private UserService userService;
    @Resource
    private TownsService townsService;
    @Resource
    private ShopService shopService;

    @Log("查询部门页面")
    @GetMapping()
//    @RequiresPermissions(value = {"module:department:department", "module:department:department:shop"})
    String Shop(Model model) {
        UserDO user = getUser();
        QueryWrapper<Towns> qwTown = new QueryWrapper<>();
        qwTown.eq("parent_id", 0);
        qwTown.eq("district_id", 1319);
        List<Towns> townsList = townsService.list(qwTown);
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 1);
        wrapper.ne("shop_type", 3);
        if (user.getTownId() != null && user.getTownId() > 0) {
            wrapper.eq("town_id", user.getTownId());
        }
        List<Shop> shopList = shopService.list(wrapper);
        model.addAttribute("townsList", townsList);
        model.addAttribute("shopList", shopList);
        model.addAttribute("user", user);
        return "module/department/department";
    }

    @Log("查询部门页面")
    @GetMapping("/{shopId}")
//    @RequiresPermissions(value = {"module:department:department", "module:department:department:shop"})
    String department(Model model, @PathVariable("shopId") Integer shopId) {
        UserDO user = getUser();
        QueryWrapper<Towns> qwTown = new QueryWrapper<>();
        qwTown.eq("parent_id", 0);
        qwTown.eq("district_id", 1319);
        List<Towns> townsList = townsService.list(qwTown);
        QueryWrapper<Shop> wrapper = new QueryWrapper<>();
        wrapper.eq("state", 1);
        wrapper.ne("shop_type", 3);
        if (user.getTownId() != null && user.getTownId() > 0) {
            wrapper.eq("town_id", user.getTownId());
        }
        List<Shop> shopList = shopService.list(wrapper);

        model.addAttribute("shopId", shopId);
        model.addAttribute("townsList", townsList);
        model.addAttribute("shopList", shopList);
        model.addAttribute("user", user);
        return "module/department/depart";
    }

    @Log("查询部门")
    @ResponseBody
    @GetMapping("/list")
//    @RequiresPermissions(value = {"module:department:department", "module:department:department:shop"})
    public DataTableLayUI<Department> list(DataTableLayUI<Department> condition) {
        UserDO user = getUser();
        Map<String, Object> map = condition.getSearchParams();
        if (user.getUserType() == 10 && user.getShopId() != null && user.getShopId() > 0) {
            map.put("shopId", user.getShopId());
        }
        if (user.getUserType() == 10 && user.getTownId() != null && user.getTownId() > 0) {
            map.put("townId", user.getTownId());
        }
        return departmentService.getListPage(condition);
    }

    @Log("新增部门页面")
    @GetMapping("/add")
    @RequiresPermissions("module:department:add")
    String add(Model model) {
        UserDO user = getUser();
        Department department = new Department();
        model.addAttribute("department", department);
        model.addAttribute("user", user);
        return "module/department/detail";
    }


    @Log("新增部门页面")
    @GetMapping("/add/{shopId}")
    @RequiresPermissions("module:department:add")
    String add(Model model,@PathVariable("shopId") Integer shopId) {
        UserDO user = getUser();
        Department department = new Department();
        department.setCompanyId(shopId);
        model.addAttribute("department", department);
        model.addAttribute("user", user);
        return "module/department/detail";
    }


    @Log("编辑部门页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:department:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        UserDO user = getUser();
        Department department = departmentService.findById(id);
        model.addAttribute("department", department);
        model.addAttribute("user", user);
        return "module/department/detail";
    }

    /**
     * 保存
     */
    @Log("新增部门")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("module:department:add")
    public R save(@Valid Department department) {
        UserDO user = getUser();
        if (user.getUserType() == 10) {
            department.setCompanyId(user.getShopId());
        }
        department.setParentId(0);
        if (departmentService.save(department)) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @Log("更新部门")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:department:edit")
    public R update(@Valid Department department) {
        UserDO user = getUser();
        if (user.getUserType() == 10) {
            department.setCompanyId(user.getShopId());
        }
        departmentService.updateById(department);
        return R.ok();
    }

    /**
     * 删除
     */
    @Log("删除部门")
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("module:department:remove")
    public R remove( Integer id) {
        if (departmentService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }


    @RequestMapping(value = "/loadByName")
    @ResponseBody
    public long loadByName(@RequestParam Map<String,Object> map) throws Exception {
        long num = 0;
        String name = map.get("name")+"";
        String deptOldName = map.get("deptOldName")+"";
        if(!deptOldName.equals(name)){
            num = departmentService.selectCountByColumn("name",name);
        }
        return num;
    }


    /**
     * 方法说明： 根据部门获取用户
     * @author Mr.Zhou
     * @date 2020/3/21
     */
    @RequestMapping(value = "/getUserByDepart")
    @ResponseBody
    public Object getUserByDepart(@RequestParam Map<String,Object> map) throws Exception {
        List<UserDO> userList =  userService.selectByMap(map);
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("code", 0);
        resultMap.put("msg", "success");
        resultMap.put("data", userList);
        return resultMap;
    }

    @ResponseBody
    @PostMapping(value = "/getDeptListByCompany")
    R getDeptListByCompany(@RequestParam("companyId") Integer companyId) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id",companyId);
        List<Department> list = departmentService.list(wrapper);
        return R.ok(0,list);
    }
}
